<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Nuhaa All Bakry, Teow Jit Huan
 *
 */

class InstallersController extends AppController {

/**
 * Define $name
 *
 */
    var $name = 'Installers';
/**
 * Define $helpers
 *
 */
    var $helpers = array('Html', 'Form','Javascript');
    
    var $uses = null;
    
    var $layout = 'install';
    var $pageTitle = 'Install';
    
    function index() {
        
    }
    
    //license
    function install(){
        

        $dfile= APP . 'LICENSE.txt';
        $file = new File($dfile);
        $this->set('license',$file->read($file->open('r')));
        $file->close();
        if (!empty($this->data)) {
                
            if(!empty($this->data['agree'])){
                //$this->testFileSystem(); 
                $this->redirect(array('action'=>'dircheck'));
            } else {
                $this->Session->setFlash('You have to accept the license to proceed');
            }
        }
    }
    
    
    //check the file permission
   function dircheck(){
        $writableDirs= array (
            ROOT . '/app/config/',
            ROOT . '/app/config/bootstrap.php',
            ROOT . '/app/tmp',
            ROOT . '/app/tmp' . DS . 'sessions',
            ROOT . '/app/tmp' . DS . 'logs',
            ROOT . '/app/tmp' . DS . 'cache',
            ROOT . '/app/tmp' . DS . 'tests',
            ROOT . '/app/webroot' . DS . 'upload',
            ROOT . '/app/webroot' . DS . 'img' . DS . 'logo',
        );
        $areNotWriteable= array ();
        $areWritable= array ();
        foreach ($writableDirs as $dir){
            if(!is_dir($dir)){
                mkdir($dir);
            }
            if (!is_writable($dir)){
                $areNotWriteable[]= $dir;
            } else { $areWritable[] = $dir; }
            unset($dir);
        }
        $this->set('areNotWriteable', $areNotWriteable);
        $this->set('areWritable', $areWritable);
        if (count($areNotWriteable)){
            $this->set(compact('areNotWriteable'));

            unset($areNotWriteable);
            //$this->render(null, null, VIEWS . low($this->name) . DS . 'testFileSystem.ctp');
            //return false;
        } 
        //    $this->redirect(array('controller'=>'installers','action'=>'database'));
        
  }
  
    //database setting
    function database(){
        $dfile= APP . 'config' . DS . 'database.php';
        $file = new File($dfile);
        
        If($this->data){
/*
            $rootpassword=$this->data['root_password'];
            unset($this->data['root_password']);
*/
            if (!($file->exists())) {
                $file->create();
            }
            $output="<?php\n";
            $output.="class DATABASE_CONFIG {\n\n";
            $output.="\tvar ".'$default'." = array(\n\n";
            $output.="\t\t'driver' => 'mysql',\n";
            foreach ($this->data as $key=>$ins){
                $output.="\t\t'".$key."' => '".$ins."',\n";
            }
            $output.="\t\t'persistent' => false,\n";
            $output.="\t\t'schema' => '',\n";
            $output.="\t\t'encoding' => ''\n";
            $output.="\t);\n";
            $output.="}\n";
            $output.="?>\n";
            
            if (!$file->writable()) return false;
            else {
                $file->open('w');
                $file->write($output);    
                $file->close();
            }
        }
        if ($file->exists()) {
            App::import('Model','ConnectionManager');
            $db= & ConnectionManager :: getDataSource('default');
            if ($db->isConnected()) {
                $this->set('host',$db->config['host']);
                $this->set('password',$db->config['password']);
                $this->set('port',$db->config['port']);
                $this->set('database',$db->config['database']);
                $this->set('login',$db->config['login']);
                $this->set('driver',$db->config['driver']);
                $this->set('prefix',$db->config['prefix']);
                
                $this->set('connected',true);
                $this->createTables();
                $this->Session->setFlash("Database is set.");
            } else {
                $this->set('connected',false);
                $this->Session->setFlash("Error connecting to database. Please make sure the database exist and username/password are correct.");
            }
        }
    }
   
   function createTables(){
        App::import('ConnectionManager');
        $db= ConnectionManager :: getDataSource('default');
        $prefix= $db->config['prefix'];
        $dfile= ROOT . DS . 'app/config/sql/mymeetingdb_tables.sql';

        $sql= file_get_contents($dfile);
        $a=0;
        
       //Changes to cater the table with prefix on 20/10/2010 - by Winnie
        $search_str = 'CREATE TABLE IF NOT EXISTS `';
        while($b=strpos($sql, $search_str ,$a)){
            $a=strpos($sql,';',$b);
           
            $exe_str = $search_str . $prefix . substr($sql,$b+28,$a-$b+1);
           //debug($exe_str);
            $db->query($exe_str);
        }
    }
    
     function change_language($language){
		$dfile= APP . 'config' . DS . 'bootstrap.php';
            $file = new File($dfile);
            $content=$file->read($file->open('rw'));
            $file->close();
    
            $content=substr_replace($content,$language,strpos($content,"define('DEFAULT_LANGUAGE','")+27,3);
            $file->write($content);    
            $file->close();

            Cache::clear();
            if(DEFAULT_LANGUAGE!=$language){
				$this->change_language($language);
			}
	}
    
    //language
    function language(){
        $this->set('languageset',false);
        
        if(!empty($this->data['language'])){
            $this->change_language($this->data['language']);
            //  debug($content);
            $this->installData();
            
            $this->Session->setFlash("Language is set.");
            $this->set('languageset',true);
        } 
        
    }
    
    //language
    /*function language(){
        $this->set('languageset',false);
        
        if(!empty($this->data['language'])){
            $dfile= APP . 'config' . DS . 'bootstrap.php';
            $file = new File($dfile);
            $content=$file->read($file->open('rw'));
            $file->close();
    
            $content=substr_replace($content,$this->data['language'],strpos($content,"define('DEFAULT_LANGUAGE','")+27,3);
            $file->write($content);    
            $file->close();

            Cache::clear();
            //  debug($content);
            $this->installData();
            
            $this->Session->setFlash("Language is set.");
            $this->set('languageset',true);
        } 
        
    }*/
    
    //install default data
    function installData(){
        App::import('ConnectionManager');
        $db= ConnectionManager :: getDataSource('default');
        //Changes to cater the table with prefix on 20/10/2010 - by Winnie
        $prefix= $db->config['prefix'];
        $a['minutetypes']="REPLACE INTO `" . $prefix. "minutetypes` (`id`, `type`, `format`, `code`) VALUES
            (1, 'MAMPU', '[%summary]<br/>[%minutes]<br/>[%signature]<br/>[%attendance]', 'mampu'),
            (2, 'OSCC', '[%summary]<br/>[%attendance]<br/>[%minutes]<br/>[%signature]', 'oscc'),
            (3, 'Lembaga Pertubuhan Peladang', '[%summary]<br/>[%attendance]<br/>[%minutes]', 'lpp')
            ;";
        $a['protocols']="REPLACE INTO `" . $prefix. "protocols` (`protocol_title`, `rank`) VALUES
            ('KSN', '1'),
            ('Turus I', '2'),
            ('Turus II', '3'),
            ('Turus III', '4'),
            ('Jusa A', '5'),
            ('Jusa B', '6'),
            ('Jusa C', '7'),
            ('54', '8'),
            ('52', '9'),
            ('48', '10'),
            ('44', '11'),
            ('41', '12'),
            ('38', '13'),
            ('32', '14'),
            ('29', '15'),
            ('27', '16'),
            ('26', '17'),
            ('22', '18'),
            ('17', '19'),
            ('11', '20'),
            ('4', '21'),
            ('3', '22'),
            ('1', '23')
            ;";
        $a['schemes']="INSERT INTO `" . $prefix. "schemes` (`name`) VALUES
            ('F'),
            ('L'),
            ('I'),
            ('J'),
            ('S'),
            ('E'),
            ('U'),
            ('N'),
            ('W'),
            ('KP'),
            ('KX'),
            ('KB'),
            ('A'),
            ('G'),
            ('C'),
            ('M'),
            ('LS'),
            ('P'),
            ('UD'),
            ('DG'),
            ('Q'),
            ('X');
            ";
            //debug (DEFAULT_LANGUAGE . $this->data['language']);
		  if(DEFAULT_LANGUAGE=='eng'){
            $a['roles']="REPLACE INTO `" . $prefix. "roles` (`id`, `name`, `deleted`, `deleted_date`) VALUES
                (1, 'admin', 0, NULL),
                (2, 'member', 0, NULL),
                (3, 'chairperson', 0, NULL);";
             $a['templates']="REPLACE INTO `" . $prefix. "templates` (`id`, `model`, `foreign_key`, `type`, `title`, `description`, `template`) VALUES
                (1, 'System', 0, 'invite', 'Meeting Invitation', 'Email which be send to invite members to attend meetings', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>INVITATION TO MEETING</span></strong></p><p>The purpose of this letter is to invite you to attend %Meeting.meeting_title. The details are shown below:</p><p>Date &amp; time: %Meeting.meeting_date</p><p>Venue: %Meeting.venue</p><p>More detail: %Link.meeting:Here</p><p>Please confirm your attendance %Link.confirm:here. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Again thank you for your support.</p>'),
                (2, 'System', 0, 'uninvite', 'Uninvitation to Meeting', 'Email which be send to uninvite meeting members', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>UNINVITATION TO MEETING<br/></span></strong></p><p>The purpose of this letter is to inform you that no need to attend %OldMeeting.meeting_title. The details are shown below:</p><p>Date &amp; time: %OldMeeting.meeting_date</p><p>Venue: %OldMeeting.venue</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (3, 'System', 0, 'change', 'Changes on Meeting', 'Email which be send if there are changes on meeting', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>CHANGES ON MEETING''S TIME/VENUE</span></strong></p><p>We regret to inform you that planned %OldMeeting.meeting_title will not take place. The previous detail are shown below:</p><p>Date &amp; time (previous): %OldMeeting.meeting_date</p><p>Venue (previous): %OldMeeting.venue</p><p>We have rescheduled the meeting. Updated meeting details are shown below:</p><p>Meeting: %Meeting.meeting_title</p><p>Date &amp; time: %Meeting.meeting_date</p><p>Venue: %Meeting.venue</p><p>More detail: %Link.meeting:here.</p><p>We apologize for any inconveniece this may cause you.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (4, 'System', 0, 'cancel', 'Cancellation of Meeting', 'Email which be send if the meeting is cancelled', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>CANCELLATION OF MEETING</span></strong></p><p>We regret to inform you that planned %Meeting.meeting_title was cancelled.</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (5, 'System', 0, 'meeting reminder', 'Meeting Reminder', 'Email which be send as reminder for the coming meeting', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>REMINDER FOR MEETING</span></strong></p><p>You are receiving this email as a reminder of the upcoming %Meeting.meeting_title scheduled for %Meeting.meeting_date in %Meeting.venue.</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (6, 'System', 0, 'status reminder', 'Status Reminder', 'Email which be send as reminder for the pending status.', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>REMINDER FOR PENDING STATUS<br /></span></strong></p><p>The purpose of this letter is to inform you that there are a pending status. The relate decision detail are shown below:</p><p>=================<br /> %Decision.description<br /> =================</p><p>which was assigned to Sir/Madam in %Meeting.meeting_title on %Meeting.meeting_date. Please update the status in %days_left day.</p><p>Please click %Link.decision:here for more detail about the related decision.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (7, 'System', 0, 'meeting comment', 'Meeting Comment', 'Email which be send if there are comment on a meeting', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF COMMENT ON MEETING<br /></span></strong></p><p>The purpose of this letter is to inform you that %Comment.user had commented Meeting %Meeting.meeting_num. The comment is shown below:</p><p>================<br /> %Comment.comment<br /> ================</p><p>For more detail on the related meeting, please click %Link.meeting:here.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (8, 'System', 0, 'decision comment', 'Decision Comment', 'Email which be send if there are comment on a decision', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF COMMENT ON DECISION<br /></span></strong></p><p>The purpose of this letter is to inform you that %Comment.user had commented decision which had been made in meeting %Meeting.meeting_num. The comment is shown below:</p><p>================<br /> %Comment.comment<br /> ================</p><p>The related decision is:</p><p>================<br /> %Decision.description<br /> ================</p><p>For more detail on the related decision, please click %Link.decision:here.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (9, 'System', 0, 'status comment', 'Status Comment', 'Email which be send if there are comment on a status', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF COMMENT ON STATUS<br /></span></strong></p><p>The purpose of this letter is to inform you that Comment.user commented status of the decision which had been made in meeting %Meeting.meeting_num. The comment is shown below:</p><p>================<br /> %Comment.comment<br /> ================</p><p>For more detail on the decision:</p><p>================<br /> %Decision.description<br /> ================</p><p>The status is:</p><p>================<br /> %Status.description<br /> ================</p><p>For more related detail, please click %Link.decision:here.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (10, 'System', 0, 'update status', 'Updating of Status', 'Email which be send to secretariat if there are a status was updated in a meeting', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF UPDATING OF STATUS<br /></span></strong></p><p>The purpose of this letter is to show you about the updated status. The details as below:</p>%%content to repeat <p>==================<br/>%Updater had updated %Status.user''s status for the decision which had been made in meeting %Meeting.meeting_num. The status is shown below:</p><p>================<br /> %Status.description<br /> ================</p><p>The related decision is shown below:</p><p>================<br /> %Decision.description<br /> ================</p><p>More detail about the decision: %Link.decision:here.</p><p>&nbsp;</p>content to repeat%%<p><b>%slogan</b></p><p>Thank you.</p>'),
                (11, 'System', 0, 'announce minute', 'Meeting Minute', 'Email which be send if the chairperson approved a meeting minute', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>MEETING MINUTE: %Meeting.meeting_num<br /></span></strong></p><p>The purpose of this letter is to inform you that %Updater had done the minute for the said meeting. </p><p>The said minute can be seen %Link.meeting:here.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (12, 'System', 0, 'decision assigned', 'Notification of Decision Assignation', 'Email which be send as notification of decision assignation', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF DECISION ASSIGNATION<br /></span></strong></p><p>The purpose of this letter is to inform you that decision had been assigned to you. Please take action. The decisions are shown below:</p>%%content to repeat<p>================<br /> %Decision.description<br /> ================</p><p>More detail: %Link.decision:here.</p><p>&nbsp;</p>content to repeat%%<p><b>%slogan</b></p><p>Thank you.</p>'),
                (13, 'System', 0, 'send for approval', 'Ask for Meeting Minute Approval', 'Email which be send to chairperson for meeting minute approval', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>ASK FOR MEETING MINUTE APPROVAL<br /></span></strong></p><p>The purpose of this letter is to inform you that secretariat had checked the minute for meeting %Meeting.meeting_num and had asked for your approval. </p><p>The minute can be seen %Link.meeting:here. Please click the link for approval process. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (14, 'System', 0, 'minutes approved', 'Notification of Meeting Minute Approval', 'Email which be send to secretariat if a meeting minute had been approved', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF MEETING MINUTE APPROVAL<br /></span></strong></p><p>The purpose of this letter is to inform you that minute for meeting %Meeting.meeting_num had been approved by the chairperson, %chairperson. </p><p>The minute can be seen %Link.meeting:here. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (15, 'SystemOnly', 0, 'forgot password', 'Forgot Password', 'Email which be send when a user had forgotten password', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>CHANGING PASSWORD<br /></span></strong></p><p>The purpose of this letter is to inform you that your reset password request in MyMeeting system had been processed. The changing of password can be done %Link.password:here. Please change your password before %due_date.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (16, 'SystemOnly', 0, 'reset password', 'New Password', 'Email which be send when a user''s password had been reset', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NEW PASSWORD<br /></span></strong></p><p>The purpose of this letter is to inform you that your reset password request in MyMeeting system had been processed. A new password had been made for you. The new password is %newpassword.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (17, 'SystemOnly', 0, 'forgot username', 'Retrieve Username', 'Email which be send when a user had forgotten username', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>RETRIEVE USERNAME<br /></span></strong></p><p>The purpose of this letter is to inform you that your retrieve username request in MyMeeting system had been processed.&nbsp; Your username is %username</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (18, 'SystemOnly', 0, 'new account', 'New Account', 'Email which be send to new system user', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>New Account<br /></span></strong></p><p>The purpose of this letter is to inform you that there is a new account which had been created for you in MyMeeting System. The login details are shown below:</p><p>Username: %username<br />Password: %newpassword</p><p>You are adviced to change the password immediately. Please login %Link.newaccount:here to update your profile.</p><p>&nbsp;</p><p>Thank you.</p>'),
                (19, 'System', 0, 'minute comment', 'Minute Comment', 'Email which be send if there are comment on a minute', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>NOTIFICATION OF COMMENT ON MINUTE<br /></span></strong></p><p>The purpose of this letter is to inform you that %Comment.user commented the minute of meeting %Meeting.meeting_num. The comments are shown below:</p>%%content to repeat<p><br />================<br />Comment:<br />================<br /> %Comment.comment<br /> ================</p><p>For more detail on the decision:</p><p>================<br /> %Decision.description<br /> ================</p>content to repeat%%<p>For more related detail, please click %Link.decision:here.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>'),
                (20, 'System', 0, 'send for reapproval', 'Ask for Meeting Minute Approval after Changes', 'Email which be send to chairperson for meeting minute approval after changes', '<p>Dear %name,</p><p><strong><span style=''text-decoration: underline;''>ASK FOR MEETING MINUTE APPROVAL AFTER CHANGES<br /></span></strong></p><p>This is to inform you that secretariat had updated the minute for meeting %Meeting.meeting_num according your comments and had asked for your approval. </p><p>The minute can be seen %Link.meeting:here. Please click the link for approval process. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Thank you.</p>');";
            $a['titles']="REPLACE INTO `" . $prefix. "titles` (`id`, `short_name`, `long_name`) VALUES
                (1, 'Y.Bhg Tan Sri', 'Y.Bhg Tan Sri'),
                (2, 'Y.Bhg Datuk', 'Y.Bhg Datuk'),
                (3, 'Y.Bhg Dato''', 'Y.Bhg Dato'''),
                (4, 'Y.Brs Dr.', 'Y.Brs Dr.'),
                (5, 'Hj.', 'Hj.'),
                (6, 'Mr.', 'Mr.'),
                (7, 'Mdm.', 'Madam'),
                (8, 'Ms.', 'Miss');";
        }else{
            $a['templates']="REPLACE INTO `" . $prefix. "templates` (`id`, `model`, `foreign_key`, `type`, `title`, `description`, `template`) VALUES
                (1, 'System', 0, 'invite', 'Jemputan ke mesyuarat', 'Emel yang dihantar untuk menjemput ahli ke mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>JEMPUTAN KE MESYUARAT</span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Sukacita dimaklumkan bahawa satu mesyuarat  %Meeting.meeting_title akan diadakan. Maklumat adalah seperti berikut:</p><p>Tarikh &amp; masa: %Meeting.meeting_date</p><p>Tempat: %Meeting.venue</p><p>Maklumat mesyuarat: %Link.meeting:Sini</p><p>3. Tuan/puan adalah dengan segala hormatnya dijemput hadir. Pengesahan kehadiran boleh dilakukan di %Link.confirm:sini. Kerjasama dan perhatian tuan/puan didahului dengan ucapan terima kasih.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (2, 'System', 0, 'uninvite', 'Tidak perlu hadir ke mesyuarat', 'Emel untuk dihantar sekiranya mesyuarat dibatalkan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>JEMPUTAN KE MESYUARAT DIBATALKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Adalah ini dimaklumkan bahawa tuan/puan tidak perlu menghadiri mesyuarat  %OldMeeting.meeting_title seperti berikut:</p><p>Tarikh &amp; masa: %OldMeeting.meeting_date</p><p>Tempat: %OldMeeting.venue</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (3, 'System', 0, 'change', 'Perubahan mesyuarat', 'Emel untuk dihantar jika terdapat perubahan kepada maklumat mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>PERUBAHAN MASA/TEMPAT MESYUARAT</span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Adalah ini dimaklumkan bahawa terdapat perubahan untuk mesyuarat  %OldMeeting.meeting_title . Maklumat lama adalah seperti berikut:</p><p>Tarikh &amp; masa (lama): %OldMeeting.meeting_date</p><p>Tempat (lama): %OldMeeting.venue</p><p><span style=''text-decoration: underline;''>Maklumat mesyuarat terkini adalah seperti berikut:</span></p><p>Mesyuarat: %Meeting.meeting_title</p><p>Tarikh &amp; masa: %Meeting.meeting_date</p><p>Tempat: %Meeting.venue</p><p>3. Maklumat mesyuarat boleh dilihat di %Link.meeting:sini. Tuan/puan adalah dengan segala hormatnya dijemput hadir. Kerjasama dan perhatian tuan/puan didahului dengan ucapan terima kasih.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (4, 'System', 0, 'cancel', 'Pembatalan mesyuarat', 'Emel untuk dihantar sekiranya mesyuarat dibatalkan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>PEMBATALAN MESYUARAT</span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Dukacita dimaklumkan bahawa mesyuarat  %Meeting.meeting_title telah dibatalkan.</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (5, 'System', 0, 'meeting reminder', 'Peringatan mesyuarat', 'Emel untuk dihantar sebagai peringatan mesyuarat yang akan datang', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>PERINGATAN UNTUK HADIR KE MESYUARAT</span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Sebagai peringatan, adalah dengan ini dimaklumkan bahawa tuan/puan telah dijemput untuk menghadiri mesyuarat  %Meeting.meeting_title pada %Meeting.meeting_date di %Meeting.venue.</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (6, 'System', 0, 'status reminder', 'Peringatan kemaskini status tindakan', 'Emel untuk menghantar peringatan kepada ahli mesyuarat untuk mengemaskini status tindakan.', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>PERINGATAN UNTUK MENGEMASKINI STATUS TINDAKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Sebagai peringatan, adalah dengan ini dimaklumkan bahawa tuan/puan masih belum mengemaskini status tindakan tuan/puan untuk keputusan mesyuarat seperti berikut:</p><p>=================<br /> %Decision.description<br /> =================</p><p>yang telah dipertanggungjwabkan kepada tuan/puan di dalam mesyuarat %Meeting.meeting_title pada %Meeting.meeting_date. Tuan/puan masih mempunyai %days_left hari lagi untuk membuat kemaskini.</p><p>3. Tuan/puan klik di  %Link.decision:sini untuk melihat maklumat keputusan.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (7, 'System', 0, 'meeting comment', 'Komen untuk mesyuarat', 'Emel untuk dihantar apabila terdapat komen yang ditinggalkan untuk mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG KOMEN YANG DITINGGALKAN UNTUK MESYUARAT<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Comment.user telah meninggalkan komen untuk mesyuarat %Meeting.meeting_num. Komennya adalah seperti berikut:</p><p>================<br /> %Comment.comment<br /> ================</p><p>3. Maklumat mesyuarat boleh dilihat di %Link.meeting:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (8, 'System', 0, 'decision comment', 'Komen untuk keputusan', 'Emel untuk dihantar apabila terdapat komen yang ditinggalkan untuk keputusan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG KOMEN YANG DITINGGALKAN UNTUK KEPUTUSAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Comment.user telah meninggalkan komen untuk keputusan yang dibuat dalam mesyuarat %Meeting.meeting_num. Komennya adalah seperti berikut:</p><p>================<br /> %Comment.comment<br /> ================</p><p>Keputusan yang dirujuk adalah:</p><p>================<br /> %Decision.description<br /> ================</p><p>3. Maklumat keputusan boleh dilihat di %Link.decision:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (9, 'System', 0, 'status comment', 'Komen untuk status', 'Emel untuk dihantar apabila terdapat komen yang ditinggalkan untuk status tindakan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG KOMEN YANG DITINGGALKAN UNTUK STATUS TINDAKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Comment.user telah meninggalkan komen untuk status tindakan yang telah diambil bagi keputusan yang dibuat dalam mesyuarat %Meeting.meeting_num. Komennya adalah seperti berikut:</p><p>================<br /> %Comment.comment<br /> ================</p><p>Keputusan yang dirujuk adalah:</p><p>================<br /> %Decision.description<br /> ================</p><p>Status tindakan yang dirujuk adalah:</p><p>================<br /> %Status.description<br /> ================</p><p>3. Maklumat keputusan dan status tindakan boleh dilihat di %Link.decision:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (10, 'System', 0, 'update status', 'Pengemaskinian status', 'Emel untuk dihantar kepada urursetia apabila terdapat status tindakan yang dikemaskinikan untuk mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG PENGEMASKINIAN STATUS TINDAKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Updater telah mengemaskinikan status %Status.user untuk keputusan yang dibuat dalam mesyuarat %Meeting.meeting_num. Statusnya adalah seperti berikut:</p><p>================<br /> %Status.description<br /> ================</p><p>Keputusan yang dirujuk adalah:</p><p>================<br /> %Decision.description<br /> ================</p><p>3. Maklumat keputusan boleh dilihat di %Link.decision:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (11, 'System', 0, 'announce minute', 'Minit mesyuarat', 'Emel untuk dihantar apabila terdapat minit mesyuarat yang telah disahkan oleh pengerusi', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MINIT MESYUARAT: %Meeting.meeting_num<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Updater telah menyiapkan minit untuk mesyuarat di atas. </p><p>3. Minit mesyuarat tersebut boleh dilihat di %Link.meeting:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (12, 'System', 0, 'decision assigned', 'Makluman tentang penugasan keputusan', 'Emel untuk dihantar apabila terdapat pengumuman keputusan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG PENUGASAN KEPUTUSAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Tuan/Puan telah ditugaskan terhadap satu keputusan. Sila ambil tindakan. Keputusan tersebut adalah seperti berikut:</p><p>================<br /> %Decision.description<br /> ================</p><p>3. Maklumat keputusan boleh dilihat di %Link.decision:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (13, 'System', 0, 'send for approval', 'Memohon kelulusan minit mesyuarat', 'Emel yang dihantar kepada pengerusi mesyuarat untuk mendapatkan kelulusan minit mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MEMOHON KELULUSAN MINIT MESYUARAT<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Pihak urusetia telah menyemak minit untuk mesyuarat %Meeting.meeting_num dan sedia untuk memohonkan kelulusan daripada pihak Tuan/Puan. </p><p>3. Minit tersebut boleh dilihat di %Link.meeting:sini. Sila klik di pautan tersebut untuk melakukan kelulusan. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (14, 'System', 0, 'minutes approved', 'Minit mesyuarat telah diluluskan', 'Emel yang dihantar kepada urusetia mesyuarat untuk memaklumkan kelulusan minit mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MINIT MESYUARAT TELAH DILULUSKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Minit untuk mesyuarat %Meeting.meeting_num telah diluluskan oleh pengerusi mesyuarat tersebut %chairperson. </p><p>3. Minit tersebut boleh dilihat di %Link.meeting:sini. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (15, 'SystemOnly', 0, 'forgot password', 'Ubahkan Kata laluan', 'Emel yang dihantar apabila ahli terlupa kata laluan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>PERUBAHAN KATA LALUAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Satu permintaan telah dilakukan di MyMeeting untuk set semula kata laluan tuan/puan. Perubahan kata laluan boleh dilakukan di %Link.password:sini. Sila mengubahkan kata laluan tuan/puan sebelum %due_date.</p><p>Harap maklum.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (16, 'SystemOnly', 0, 'reset password', 'Kata laluan baru', 'Emel yang dihantar apabila kata laluan disetkan semula', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>KATA LALUAN BARU<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Satu permintaan telah dilakukan di MyMeeting untuk set semula kata laluan tuan/puan. Oleh yang demikian, kata laluan baru telah dijana untuk kegunaan tuan/puan. Kata laluan baru tuan/puan ialah %newpassword.</p><p>Harap maklum.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (17, 'SystemOnly', 0, 'forgot username', 'Dapatkan semula kata nama', 'Emel yang dihantar apabila ahli terlupa kata nama', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MENDAPATKAN SEMULA KATA NAMA<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Satu permintaan telah dilakukan di MyMeeting untuk mendapatkan semula kata nama tuan/puan.&nbsp; Kata nama tuan/puan ialah %username</p><p>Harap maklum.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (18, 'SystemOnly', 0, 'new account', 'Akaun telah didaftarkan', 'Emel yang dihantar kepada pengguna sistem yang baru didaftarkan', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>AKAUN ANDA TELAH DIDAFTARKAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Pentadbir sistem MyMeeting telah mendaftarkan nama anda sebagai pengguna sistem. Maklumat log masuk anda adalah seperti berikut:</p><p>Kata nama: %username<br />Kata laluan: %newpassword</p><p>Anda dinasihatkan untuk menukar kata laluan anda. Sila log masuk di %Link.newaccount:sini untuk mengemaskini profail anda.</p><p>&nbsp;</p><p>Terima kasih.</p>'),
                (19, 'System', 0, 'minute comment', 'Komen untuk minit', 'Emel untuk dihantar apabila terdapat komen yang ditinggalkan untuk minit mesyuarat', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MAKLUMAN TENTANG KOMEN YANG DITINGGALKAN UNTUK MINIT MESYUARAT<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. %Comment.user telah meninggalkan komen untuk minit mesyuarat %Meeting.meeting_num. Komen-komennya adalah seperti berikut:</p>%%content to repeat<p><br/>================<br />Komen:<br/>================<br /> %Comment.comment<br /> ================</p><p>Keputusan yang dirujuk adalah:</p><p>================<br /> %Decision.description<br /> ================</p>content to repeat%%<p>3. Maklumat minit mesyuarat boleh dilihat di %Link.decision:sini.</p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>'),
                (20, 'System', 0, 'send for reapproval', 'Memohon kelulusan minit mesyuarat selepas pengemaskinian', 'Emel yang dihantar kepada pengerusi mesyuarat untuk mendapatkan kelulusan minit mesyuarat selepas pengemaskinian', '<p>ASSALAMUALAIKUM DAN SALAM SEJAHTERA</p><p>%name,</p><p><strong><span style=''text-decoration: underline;''>MEMOHON KELULUSAN MINIT MESYUARAT SELEPAS PENGEMASKINIAN<br /></span></strong></p><p>Dengan segala hormatnya merujuk perkara di atas.</p><p>2. Pihak urusetia telah mengemaskininkan minit untuk mesyuarat %Meeting.meeting_num dengan merujukkan komen anda dan sedia untuk memohonkan kelulusan daripada pihak Tuan/Puan. </p><p>3. Minit tersebut boleh dilihat di %Link.meeting:sini. Sila klik di pautan tersebut untuk melakukan kelulusan. </p><p>&nbsp;</p><p><b>%slogan</b></p><p>Terima kasih.</p>');";
            $a['titles']="REPLACE INTO `" . $prefix. "titles` (`id`, `short_name`, `long_name`) VALUES
                (1, 'Y.Bhg Tan Sri', 'Y.Bhg Tan Sri'),
                (2, 'Y.Bhg Datuk', 'Y.Bhg Datuk'),
                (3, 'Y.Bhg Dato''', 'Y.Bhg Dato'''),
                (4, 'Y.Brs Dr.', 'Y.Brs Dr.'),
                (5, 'Hj.', 'Hj.'),
                (6, 'En.', 'En.'),
                (7, 'Pn.', 'Pn.'),
                (8, 'Cik', 'Cik');";
            $a['roles']="REPLACE INTO `" . $prefix. "roles` (`id`, `name`, `deleted`, `deleted_date`) VALUES
                (1, 'admin', 0, NULL),
                (2, 'ahli', 0, NULL),
                (3, 'pengerusi', 0, NULL);";
        }
       
        foreach($a as $query){
            $db->query($query);
        }        
    }
    
    //setting the agency detail
    function syssettings(){
        
        $dfile= APP . 'config' . DS . 'mymeeting.php';
        $file = new File($dfile);
        if (!($file->exists())) $file->create();
        
        $this->set('agency_name',Configure::read('agency_name'));
        $this->set('agency_address',Configure::read('agency_address'));
        $this->set('agency_slogan',Configure::read('agency_slogan'));
        $this->set('server_url',Configure::read('server_url'));
        $this->set('email_from',Configure::read('email_from'));
        $this->set('email_from_name',Configure::read('email_from_name'));
        
        $this->set('syssettingset',false);
        if (!empty($this->data)) {

            $output="<?php\n";
            $output.="\t/* Usage: \n";
            $output.="\t * Configure::write('One.key1', 'value of the Configure::One[key1]');\n";
            $output.="\t * Configure::read('Name'); will return all values for Name\n";
            $output.="\t * Configure::read('Name.key'); will return only the value of Configure::Name[key]\n";
            $output.="\t */\n\n";
        
            $unset=array('name','username','password','password_confirm','superuser','email');
            foreach($this->data as $mid=>$mdata){
                if(!in_array($mid,$unset)){
                    $output.="\tConfigure::write('$mid','$mdata');\n";
                }
            }
            $output.="?>\n";
            
            if (!$file->writable()) return false;
            else {
                $file->open('w');
                $file->write($output);    
                $file->close();
                //$this->redirect(array('controller'=>'installers','action'=>'install3'));
                $this->Session->setFlash("System settings are set.");
                $this->set('syssettingset',true);
            }
        }
        
        if ($this->addUser()) {
            $this->redirect(array('action'=>'success'));
        }
    }
    

    //admin detail
    function addUser(){
        App::import('Model','User');
        $this->User=&new User;
        
        if (!empty($this->data)) {
            
            //validation
            $errormsg=null;
            $minlength=array(__('Username',true)=>$this->data['username'],__('Password','true')=>$this->data['password']);
            foreach($minlength as $field=>$data){
                if(strlen($data)<4){
                    $errormsg.=$field." : ".'Minimum length 4';
                    $errormsg.='<br/>';
                }elseif(!preg_match('/^([a-zA-Z0-9])+$/', $data)){
                    $errormsg.=$field." : ".'Alphabets and numbers only';
                    $errormsg.='<br/>';
                }
            }
            $notempty=array(__('Name',true)=>$this->data['name'],__('Email',true)=>$this->data['email']);
            foreach($notempty as $nfield=>$ndata){
                if(empty($ndata)){
                    $errormsg.=$nfield." : ".'This field cannot be left blank';
                    $errormsg.='<br/>';
                }
            }
            $emailfield=array('Email'=>$this->data['email']);
            
            foreach($emailfield as $efield=>$uemail){
                if((!preg_match('/^([_a-zA-Z0-9.]+@[-a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)+)*$/', $uemail))){
                    $errormsg.=$efield." : ".'Invalid email format';
                    $errormsg.='<br/>';
                }
            }
            if ($this->data['password'] != $this->data['password_confirm']) {
                $errormsg.="Password are not identical.";
                $errormsg.='<br/>';
            }
                
            if ($errormsg ==null) {
                $this->data['password']=$this->Auth->password($this->data['password']);
                $this->User->create();
                if ($this->User->save($this->data)) {
                    return true;
                }else{
                    $this->Session->setFlash('Administrator could not be created. Please try again.');
                    return false;
                }
            }else {
                $this->Session->setFlash($errormsg);
                return false;
            }
        }
        
    }
   
   //success and register
    function success(){
        if(!empty($this->data['purpose'])){
            if($this->data['purpose']=='production'){
                $message=
                    "<p>An agency ".configure::read('agency_name')." has successfully installed MyMeeting for PRODUCTION. 
                    Below is the detail of the agency:</p>
                    <p><br/>
                    Agency name: ".configure::read('agency_name')."<br/>
                    Email: ".$this->data['email']."<br/>
                    </p>";
                App::import('ConnectionManager');
                $db= ConnectionManager :: getDataSource('default');
                $db->query("
                    INSERT INTO `" . $prefix. "notifications` (`meeting_id`, `type`, `message_title`, `notification_date`, `notification_sent`, `message`, `to`) VALUES
                    (0, 'register','MyMeeting New Installation','".date('Y-m-d H:m:s')."', 0, '".$message."', 'helpdesk@oscc.org.my');
                  ");
            } else {
                if ($this->data['contactme'] == 'Yes') {
                    
                $message=
                    "<p>An agency ".configure::read('agency_name')." has successfully installed MyMeeting for REVIEW and they wish for OSCC to contact them.  
                    Below is the detail of the agency:</p>
                    <p><br/>
                    Agency name: ".configure::read('agency_name')."<br/>
                    Contact: ".$this->data['name']."<br/>
                    Email: ".$this->data['email']."<br/>
                    Phone: ".$this->data['phone']."<br/>
                    Has intention to deploy for production: ".$this->data['intent']."
                    ";
                } else {
                    
                $message=
                    "<p>An agency ".configure::read('agency_name')." has successfully installed MyMeeting for REVIEW.  
                    Below is the detail of the agency:</p>
                    <p><br/>
                    Agency name: ".configure::read('agency_name')."<br/>
                    Contact: ".$this->data['name']."<br/>
                    Email: ".$this->data['email']."<br/>
                    Phone: ".$this->data['phone']."<br/>
                    Has intention to deploy for production: ".$this->data['intent']."
                    ";
                }
                App::import('ConnectionManager');
                $db= ConnectionManager :: getDataSource('default');
				//Changes to cater the table with prefix on 20/10/2010 - by Winnie
				$prefix= $db->config['prefix'];
                $db->query("
                    INSERT INTO `" . $prefix. "notifications` (`meeting_id`, `type`, `message_title`, `notification_date`, `notification_sent`, `message`, `to`) VALUES
                    (0, 'register','MyMeeting New Installation','".date('Y-m-d H:m:s')."', 0, '".$message."', 'helpdesk22@oscc.org.my');
                  ");
            }
          
            $this->redirect(array('controller'=>'committees','action'=>'mainpage'));
        }
    }
}
?>
